package per.qiao.simplerest.scanner;

import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import per.qiao.simplerest.anno.RestMapping;

import java.lang.annotation.Annotation;
import java.util.Set;

/**
 * Company: 上海数惠系统技术有限公司
 * Department: 数据中心
 * Date: 2020-05-29 13:28
 *
 * @Author: qyp
 * Email: qiaoyp@dist.com.cn
 * Desc: restMapping注解扫描器
 */
public class RestMappingScanner extends AbstractRestServiceScanner {

    /**
     * 支持的注解
     */
    private final Class supportAnno = RestMapping.class;

    @Override
    public boolean supports(Class clazz) {
        if (!clazz.isAnnotation() && matched(clazz)) {
            return true;
        }
        return false;
    }

    private boolean matched(Class pkgClass) {
        if (pkgClass == null) {
            return false;
        }

        Annotation annotation = pkgClass.getAnnotation(supportAnno);
        if (annotation != null) {
            return true;
        }

        Class[] interfaces = pkgClass.getInterfaces();
        if (!ObjectUtils.isEmpty(interfaces)) {
            for (Class aClass : interfaces) {
                matched(aClass);
            }
        }
        Class superclass = pkgClass.getSuperclass();
        return matched(superclass);
    }

    @Override
    protected Set<Class> innerScanner(Set<Class> allMetcher) {
        return allMetcher;
    }


}
